<?php
namespace admin\controller;

use \core\Controller;
header("content-type:text/html;charset=utf-8");
class CatController extends Controller{
    //列表页
    public function showList(){
        $model=SF('\\model\\CategoryModel');
        $rows=$model->getRows('id,name,parent_id','bl_category','1');

        //调用无限级变量
        $tree=array();
        $model->recursiveCat($tree,$rows);


        //分配变量
        $this->assign('rows',$tree);
        $this->display('Category/categoryIndex.html');
    }




    //新增页
    public function showAd(){
        $model=SF('\\model\\CategoryModel');
        $categorys=$model->getRows('id,name,parent_id','bl_category','1');

        //调用无限级变量
       $tree=array();
        $model->recursiveCat($tree,$categorys);


        //分配变量
        $this->assign('categorys',$tree);

        $this->display('Category/categoryAdd.html');
    }
    //递归
//         private function recursiveCat(&$tree,$cateforys,$c_id=0,$level=0){
//        foreach ($cateforys as $k=>$category){
//            if($category['parent_id']==$c_id){
//                $category['level']=$level;
//                $tree[]=$category;
////                print_r($category); echo '<hr/>';
//            $this->recursiveCat($tree,$cateforys,$category['id'],$level+1);
//            }
//        }
//     }


    //新增方法
    public function ad(){
        //接受页面参数
        $name=$_POST['name'];
        $parent_id=$_POST['parent_id'];
        //调用模型
        $model=SF('\\model\\CategoryModel');
        $vals="null,'$name','$parent_id'";
        if($model->ad('bl_category',$vals)){
           $this->jump('新增分类成功','p=admin&m=Cat&a=showList',1);
        }else{
            $this->jump('新增分类失败','p=admin&m=Cat&a=showList');
        }
    }

    //更新页
    public function showUpd(){
        $id=$_GET['id'];
        $model=SF('\\model\\CategoryModel');
        $row=$model->getRow('id,name,parent_id','bl_category','id='.$id); //当前记录
        $rows=$model->getRows('id,name,parent_id','bl_category','1');//所有有记录

        $tree=array();
        $model->recursiveCat($tree,$rows);
        //分配 渲染
        $this->assign('tree',$tree);
        $this->assign('row',$row);

        $this->display('Category/categoryEdit.html');
    }

    //更新方法
        public  function upd(){
                $id=$_GET['id'];
                $name=$_POST['name'];
                $parent_id=$_POST['parent_id'];

                $model=SF('\\model\\CategoryModel');
                $vals="name='$name',parent_id='$parent_id'";
                if($model->upd('bl_category',$vals,'id='.$id))
               {
                   echo$this->jump('更新成功','p=admin&m=Cat&a=showList',1);
               }else{
                   echo$this->jump('更新失败','p=admin&m=Cat&a=showList',1);
               }

    }

    //删除功能
        public function del(){
            $id=$_GET['id'];
            $model=SF('\\model\\CategoryModel');
            //判断此函数是否有子类
            $row=$model->getrow('count(*) as num','bl_category','parent_id='.$id);
            if($row['num']==0){

                if($model->del('bl_category','id='.$id)) {
                    echo $this->jump('删除成功','p=admin&m=Cat&a=showList',1);

                }else {
                        echo $this->jump('删除失败','p=admin&m=Cat&a=showList',1);
                }
            }else{
             echo $this->jump('此类还有子分类不能删除','p=admin&m=Cat&a=showList',1);
            }
        }

}
